package engine.loader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import engine.Engine;
import engine.Logger;

public class FileLoader {
	private static final String CONFIG_DIR = "/settings/";

	private Engine engine;
	private Logger logger;
	private BufferedReader fileReader;
	private String fullFilePath;

	public FileLoader(Engine engine, String fileName) {
		this.engine = engine;
		logger = Logger.getInstance();
		fullFilePath = CONFIG_DIR + fileName;
	}

	public final void parse() {
		try {
			InputStream in = getClass().getResourceAsStream(fullFilePath);
			fileReader = new BufferedReader(new InputStreamReader(in));
			parseRule();
			fileReader.close();
		}
		catch (NullPointerException e) {
			log(Logger.ERROR, "Error reading file: " + fullFilePath);
			System.exit(1);
		}
		catch (IOException e) {
			log(Logger.ERROR, "Error reading file: " + fullFilePath);
			System.exit(1);
		}
	}

	protected void parseRule() throws IOException {}

	protected void log(int type, String message) {
		logger.out(type, message);
	}

	public BufferedReader getFileReader() {return fileReader;}

	public Engine getEngine() {return engine;}

}